#include "usr_inc.h"

#define BUF5_MAX_SIZE 16

/* 定义uart5接收体实例 */
UartRx_t rx5;
uint8_t buf5No1[BUF5_MAX_SIZE];
uint8_t buf5No2[BUF5_MAX_SIZE];
uint8_t buf5No3[BUF5_MAX_SIZE];
uint8_t buf5No4[BUF5_MAX_SIZE];

/**
 * @brief 初始化所有串口接收体实例
 */
void ALL_BSP_UART_Init(void)
{
    BSP_UART_Init(&rx5, &huart5);
    HAL_UART_Receive_IT(rx5.huart, &rx5.data, 1);
}

/**
 * @brief 重定义串口接收回调函数
 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart == &huart5)
    {
        if (rx5.state == 0 && rx5.data == '@')
        {
            rx5.state = 1;
        }
        else if (rx5.state == 1 && rx5.data == '#')
        {
            rx5.state = 2;
        }
        else if (rx5.state == 2)
        {
            if (rx5.cnt >= BUF5_MAX_SIZE)
            {
                rx5.state = 0;
                rx5.cnt = 0;
            }

            if (rx5.data == '$')
            {
                buf5No1[rx5.cnt] = '\0';

                rx5.state = 3;
                rx5.cnt = 0;
            }
            else
            {
                buf5No1[(rx5.cnt)++] = rx5.data;
            }
        }
        else if (rx5.state == 3)
        {
            if (rx5.cnt >= BUF5_MAX_SIZE)
            {
                rx5.state = 0;
                rx5.cnt = 0;
            }

            if (rx5.data == '%')
            {
                buf5No2[rx5.cnt] = '\0';

                rx5.state = 4;
                rx5.cnt = 0;
            }
            else
            {
                buf5No2[(rx5.cnt)++] = rx5.data;
            }
        }
        else if (rx5.state == 4)
        {
            if (rx5.cnt >= BUF5_MAX_SIZE)
            {
                rx5.state = 0;
                rx5.cnt = 0;
            }

            if (rx5.data == '^')
            {
                buf5No3[rx5.cnt] = '\0';

                rx5.state = 5;
                rx5.cnt = 0;
            }
            else
            {
                buf5No3[(rx5.cnt)++] = rx5.data;
            }
        }
        else if (rx5.state == 5)
        {
            if (rx5.cnt >= BUF5_MAX_SIZE)
            {
                rx5.state = 0;
                rx5.cnt = 0;
            }

            if (rx5.data == '&')
            {
                buf5No4[rx5.cnt] = '\0';

                PidArgsMsg_t args = {0};
                args.target = atof(buf5No1);
                args.kp = atof(buf5No2);
                args.ki = atof(buf5No3);
                args.kd = atof(buf5No4);
                xQueueSendToBackFromISR(pidArgsQueueHandle, &args, NULL);

                rx5.state = 0;
                rx5.cnt = 0;
            }
            else
            {
                buf5No4[(rx5.cnt)++] = rx5.data;
            }
        }

        HAL_UART_Receive_IT(rx5.huart, &rx5.data, 1);
    }
}

/**
 * @brief 重定向printf到USART5
 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(&huart5, (uint8_t *)&ch, 1, 0xFFFF);
    return ch;
}
